package com.zx.mes.controller;

import com.zx.mes.mapper.UserMapper;
import com.zx.mes.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * controller
 * @author huayunlong
 * @date 2018-6-6
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    List<User> userList = new ArrayList<>();

    @PutMapping("/")
    public void add(User user){
        userList.add(user);
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable String id){
        User u=new User();
        for (User user : userList) {
            if (user.getId().equalsIgnoreCase(id)) {
                u= user;
            }
        }
        return u;
    }

    @PostMapping("/")
    public void edit(User u){
        for (int i = 0; i <userList.size() ; i++) {
            if (userList.get(i).getId().equalsIgnoreCase(u.getId())){
                userList.set(i, u);
            }
        }
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id){
        for (int i = 0; i <userList.size() ; i++) {
            if (userList.get(i).getId().equalsIgnoreCase(id)){
                userList.remove(i);
            }
        }
    }


    @GetMapping("/getAll")
    @ResponseBody
    public List<com.zx.mes.entity.User> getAll(){
        return userMapper.getAll();
    }

}
